﻿using System;
using System.Globalization;

namespace Sustainalytics.Entities.ProductInvolvement
{
    public class PILevelOfInvolvement
    {
        public virtual int Id { get; set; }
        public virtual string Text { get; set; }
        private double _start;
        public virtual double Start
        {
            get { return _start; }
            set {
                _start = value;
                double[] lvls = new double[] { 0, 5, 10, 25, 50 };
                Id = Array.IndexOf(lvls, _start) + 1;
            }
        }

        public virtual double Stop { get; set; }

        public static PILevelOfInvolvement FromString(string s)
        {
            var result = new PILevelOfInvolvement() { Start = -1, Stop = -1 };
            if (!string.IsNullOrEmpty(s))
            {
                var vals = s.Split(new char[] { '-', '%' }, StringSplitOptions.RemoveEmptyEntries);
                if (vals.Length == 2)
                {
                    double start;
                    double stop;
                    if (double.TryParse(vals[0], out start) && double.TryParse(vals[1], out stop))
                    {
                        result.Start = start;
                        result.Stop = stop;
                    }
                    result.Text = s;
                }
            }
            return result;
        }

        public static PILevelOfInvolvement FromId(int id)
        {
            var result = new PILevelOfInvolvement() { Id = id };

            switch(id)
            {
                case 1:
                    result._start = 0;
                    result.Stop = 4.9;
                    break;
                case 2:
                    result._start = 5;
                    result.Stop = 9.9;
                    break;
                case 3:
                    result._start = 10;
                    result.Stop = 24.9;
                    break;
                case 4:
                    result._start = 25;
                    result.Stop = 49.9;
                    break;
                case 5:
                    result._start = 50;
                    result.Stop = 100;
                    break;
                default:
                    result._start = -1;
                    result.Stop = -1;
                    break;
            }

            if (1 <= id && id <= 5)
                result.Text = $"{result.Start.ToString("G", CultureInfo.InvariantCulture)}-{result.Stop.ToString("G", CultureInfo.InvariantCulture)}%";

            return result;
        }
    }
}

